package czopyk.model.structure;

import model.Time;

import org.apache.log4j.Logger;


public class CenterLoadCounter {

	private Logger log = Logger.getLogger(getClass());
	
	private double load;
	
	private double lastUpdateTime;
	
	private double lastResetTime;
	
	private final String name;
	
	public CenterLoadCounter(String name) {
		super();
		this.name = name;
		reset();
	}

	public void reset() {
		load = 0.0;
		lastUpdateTime = lastResetTime = Time.currentTime();
	}

	public double getLoad() {
		return load;
	}
	
	public double getLoadAndReset() {
		double load;
		if (Time.currentTime() == lastResetTime) {
			load = 0.0;
		} else {
			load = this.load / (Time.currentTime() - lastResetTime);
		}
		reset();
		return load;
	}
	
	public void registerLoadSinceLastTime(int channels) {
		load += (Time.currentTime() - lastUpdateTime) * channels;
		if (log.isDebugEnabled()) {
			log.debug("Updating utilization - sum is: " + load);
		}
		lastUpdateTime = Time.currentTime();
	}
	
}
